import Vue from 'vue'
import VueRouter from 'vue-router'
import HomeView from '../views/DEVLayout.vue'

Vue.use(VueRouter)
export const dyroutes = [
    {
        path: '',
        name: '主框架',
        component: HomeView,
        redirect: '/welcome',
        children: [
            {path: 'city', name: '城市', component: () => import('@/views/City.vue')},
            {path: 'welcome', name: '欢迎主页', component: () => import('@/views/Welcome.vue')},
            {path: 'about', name: '关于', component: () => import('../views/AboutView.vue')},
            {path: 'ryxx', name: '人员信息', component: () => import('@/views/Ryxx.vue')},
            {path: 'yjssQx', name: '油井实时曲线', component: () => import('../views/YjssQx.vue')},
            {path: 'menu', name: '菜单管理', component: () => import('../views/Menu.vue')},
            {path: 'role', name: '角色管理', component: () => import('../views/Role.vue')},
            {
                path: 'user', name: '用户管理', component: () => import('../views/User.vue'),
                children: [{
                    path: 'zb',
                    name: '指标下载',
                    component: () => import('../views/ZhiBiao.vue')
                }]
            }
        ]
    }
]
var i = 0
function delRoutes( dyroutes, menus){
    i++
    if(dyroutes == null || dyroutes.length == 0)
        return
    console.log('i='+i)
    var newArr = []
    for(var route in dyroutes){
        if(menus.includes(route.name)){
            newArr.push(route)
        }
    }
    console.log('newArr = ' + newArr)
    console.log()

    dyroutes.length = 0
    for(var route in newArr){
        dyroutes.push(route)
    }
    if(dyroutes.length > 0){
        for(var route in dyroutes){
            delRoutes(route.children, menus)
        }
    }
}
function setRoutes (){
    // var menus = sessionStorage.getItem('menu')
    var menus = ['主框架', '菜单管理']

    delRoutes(dyroutes, menus)
    console.log(dyroutes)
}
setRoutes()
const routes = [
    ...dyroutes,
    {
        path: '/zb',
        name: '指标下载',
        component: () => import('../views/ZhiBiao.vue')
    },
    // {
    //     path: '/about',
    //     name: '关于',
    //     // route level code-splitting
    //     // this generates a separate chunk (about.[hash].js) for this route
    //     // which is lazy-loaded when the route is visited.
    //     component: () => import(/* webpackChunkName: "about" */ '../views/AboutView.vue')
    // },
    {
        path: '/login',
        name: '登录',

        // route level code-splitting
        // this generates a separate chunk (about.[hash].js) for this route
        // which is lazy-loaded when the route is visited.
        component: () => import('../views/Login.vue')
    }
]

const router = new VueRouter({
    routes
})
// 注意，这里是测试使用，直接放行所有，正式版请打开注释
router.beforeEach((to, from, next) => {
    if (to.path == '/login') {
        next()
        return
    }
    if (to.path == '/zb') {
        next()
        return
    }

    if (!sessionStorage.getItem('user')) {
        next('/login')
    } else {
        next()
    }

})
// 这段代码是网上拷贝的，去掉的话如果用this.$router.push跳转到相同地址，就会出现错误：NavigationDuplicated：Avoided redundant navigation to current location
// 从网上百度的解决方法：vue项目NavigationDuplicated：Avoided redundant navigation to current location（重复请求相同路径警告）解决办法
// 从代码看似乎只是用catch隐藏了错误
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
    return originalPush.call(this, location).catch(err => err)
}


export default router
